package com.leetcode.algorithm.y22.m07;

/**
 * 303. 区域和检索 - 数组不可变
 * 
 * https://leetcode.cn/problems/range-sum-query-immutable/
 * 
 * @author jie.deng
 *
 */
class NumArray {
	
	private int[] prefixSumArr;

	public NumArray(int[] nums) {
		prefixSumArr = new int[nums.length];
		prefixSumArr[0] = nums[0];
		for (int i = 1; i < nums.length; i++) {
			prefixSumArr[i] = nums[i] + prefixSumArr[i - 1];
		}
	}

	public int sumRange(int left, int right) {
		if (left == 0) {
			return prefixSumArr[right];
		}
		return prefixSumArr[right] - prefixSumArr[left - 1];
	}
}
/**
 * Your NumArray object will be instantiated and called as such:
 * NumArray obj = new NumArray(nums);
 * int param_1 = obj.sumRange(i,j);
 */